(function() {
	var get = Ember.get,
		set = Ember.set,
		merge = Ember.merge;
	var map = Ember.EnumerableUtils.map,
		forEach = Ember.EnumerableUtils.forEach;

	APM.Adapter = Ember.Object.extend({
		namespace: ADAPTER_URL,
		buildURL: function(command, params) {
			var host = get(this, 'host'),
				namespace = get(this, 'namespace'),
				url = [],
				query = {};

			if (host) {
				url.push(host);
			}
			if (namespace) {
				url.push(namespace);
			}

			if(params){
				$.extend(query, params);
			}

			//用于开发
			if( isLocalDebugModel ){
			}

			url = url.join('/');

			url += "?" + this.buildPrams(query);

			return url;
		},
		//该方法主要代码来自jQuery.param
		buildPrams: function(params){
			var rbracket = /\[\]$/,
				prefix,
				s = [],
				add = function( key, value ) {
					// If value is a function, invoke it and return its value
					value = jQuery.isFunction( value ) ? value() : ( value == null ? "" : value );
					//去掉对参数名的编码
					s[ s.length ] = key + "=" + encodeURIComponent( value );
				},
				_buildParams = function( prefix, obj, add ) {
					var name;

					if ( jQuery.isArray( obj ) ) {
						// Serialize array item.
						jQuery.each( obj, function( i, v ) {
							if ( rbracket.test( prefix ) ) {
								// Treat each array item as a scalar.
								add( prefix, v );

							} else {
								// Item is non-scalar (array or object), encode its numeric index.
								_buildParams( prefix + "[" + ( typeof v === "object" ? i : "" ) + "]", v, add );
							}
						});

					} else if ( jQuery.type( obj ) === "object" ) {
						// Serialize object item.
						for ( name in obj ) {
							_buildParams( prefix + "[" + name + "]", obj[ name ], add );
						}

					} else {
						// Serialize scalar item.
						add( prefix, obj );
					}
				};

			// If an array was passed in, assume that it is an array of form elements.
			if ( jQuery.isArray( params ) || ( params.jquery && !jQuery.isPlainObject( params ) ) ) {
				// Serialize the form elements
				jQuery.each( params, function() {
					add( this.name, this.value );
				});

			} else {
				for ( prefix in params ) {
					if(params[ prefix ] != ""){
						_buildParams( prefix, params[ prefix ], add );
					}
				}
			}

			// Return the resulting serialization
			return s.join( "&" );
		},
		ajax: function(url, type, data) {
			var adapter = this;

			return new Ember.RSVP.Promise(function(resolve, reject) {
				var hash = {};
				hash.url = url;
				hash.type = type || "GET";
				hash.dataType = 'json';
				hash.context = adapter;
				hash.cache = false;
				hash.data = data;

				if (adapter.headers !== undefined) {
					var headers = adapter.headers;
					hash.beforeSend = function(xhr) {
						forEach.call(Ember.keys(headers), function(key) {
							xhr.setRequestHeader(key, headers[key]);
						});
					};
				}

				hash.success = function(json) {
					resolve.call(null, json);
					//Ember.run(null, resolve, json);
				};

				hash.error = function(jqXHR, textStatus, errorThrown) {
					if (jqXHR) {
						jqXHR.then = null;
					}
					reject.call(null, jqXHR);

					//Ember.run(null, reject, jqXHR);
				};

				Ember.$.ajax(hash);
			});
		}
	});

	Ember.onLoad('Ember.Application', function(Application) {
		Application.initializer({
			name: "newCloudAdapter",

			initialize: function(container, application) {
				application.register('adapter:apm', APM.Adapter);
			}
		});
	});
})();